/// <reference path="jquery.js" />

function Search() {
    var obj = $("#keyword");
    var topic = obj.data("topic")
    var keyword = trim(obj.val());
    if (keyword == "") {
        alert("Please enter the keyword");
        return;
    }
    if (typeof (topic) != "undefined")
        location.href = "/search?q=" + encodeURIComponent(keyword) + "&topic=" + encodeURIComponent(topic);
    else
        location.href = "/search?q=" + encodeURIComponent(keyword);
}


function trim(x) {
    return x.replace(/^\s+|\s+$/gm, '');
}


function Login(form) {

    $.ajax({
        type: "post",
        url: "/ajax/login?rdx=" + Math.random(),
        data: { rd: Math.random() },
        datatype: "json",
        success: function (res) {
            if (res.IsSuss == false) {

                if (res.Message.indexOf("http://") >= 0) {
                    location.href = res.Message;
                    return;
                }

                setTimeout(function () { Login(form) }, 1500);

                return;
            }

            if (form == "0") {
                location.href = "/";
            }
            else {
                location.href = form;
            }
        },
        error: function () {

        }
    });
}


if (!Array.prototype.forEach) {
    Array.prototype.forEach = function forEach(callback, thisArg) {
        var T, k;
        if (this == null) {
            throw new TypeError("this is null or not defined");
        }
        var O = Object(this);
        var len = O.length >>> 0;
        if (typeof callback !== "function") {
            throw new TypeError(callback + " is not a function");
        }
        if (arguments.length > 1) {
            T = thisArg;
        }
        k = 0;
        while (k < len) {

            var kValue;
            if (k in O) {
                kValue = O[k];
                callback.call(T, kValue, k, O);
            }
            k++;
        }
    };
}

function initHighlight() {
    $("pre").each(function () {
        var obj = $(this);
        var preClassName = obj.attr("class");
        var className = "";
        if (preClassName.indexOf("brush:html") >= 0) className = "language-html";
        else if (preClassName.indexOf("brush:c#") >= 0) className = "language-csharp";
        else if (preClassName.indexOf("brush:java") >= 0) className = "language-java";
        else if (preClassName.indexOf("brush:php") >= 0) className = "language-php";
        else if (preClassName.indexOf("brush:sql") >= 0) className = "language-sql";
        else if (preClassName.indexOf("brush:css") >= 0) className = "language-css";
        else if (preClassName.indexOf("brush:go") >= 0) className = "language-go";
        else if (preClassName.indexOf("brush:cpp") >= 0) className = "language-cpp";
        else if (preClassName.indexOf("brush:python") >= 0) className = "language-python";
        else if (preClassName.indexOf("brush:typescript") >= 0) className = "language-typescript";
        else className = "language-javascript";
        var code = obj.find("code");
        if (typeof (code.attr("class")) == 'undefined') {
            code.addClass(className);
        }
    });

    if (typeof (hljs)!="undefined") hljs.initHighlightingOnLoad();
}

$(document).ready(function () {
    initHighlight();
    $("#keyword").keydown(function (e) {
        var curKey = e.which;
        if (curKey == 13) {

            Search();
            return false;
        }
    });

    //setTimeout(function () {
    //    $(".pagew").css("overflow-y", "auto");
    //    $(".pagew").height($(window).height() - 80);
    //}, 1);

});

